class Solution {
    public int maxSubArray(int[] nums) {
        // 创建dp表、初始化、填表、返回值
        int n = nums.length;
        int[] dp = new int[n];

        dp[0] = nums[0];

        for (int i = 1; i < n; i++) {
            dp[i] = Math.max(nums[i], dp[i-1]+nums[i]);
        }

        // 可能会出现最大数组和为负数的情况，不能初始化为0
        int ret = Integer.MIN_VALUE;
        for (int i = 0; i < n; i++) {
            ret = Math.max(ret, dp[i]);
        }

        return ret;
    }
}